import { getRouteForm } from '../../api/manage'
import create from 'dd-store'
import pagesStore from '../../stores/globalStore'
create.Page({
  store: pagesStore,
  data: {
    discuss: [],
    group: [],
    list: [],
    selectedList: [],
    defaultCheckList: []
  },
  onLoad(options) {

    this.setData({
      list: options.launch
    })
    let data = {
      pmPeriodCode: options.code
    }

    let newArr = []
    this.$store.data.launchShowList.forEach((item) => {
      newArr.push(item.value)
    })
    this.setData({

      defaultCheckList: newArr,
      group: newArr
    })
    console.log(this.data.selectedList);
    getRouteForm(data).then(rs => {
      let newList = rs.data.map((item, index) => {
        return Object.assign({}, { label: item.pmGroupName }, { value: item.pmGroupId })
      })
      let checkList = []
      newList.forEach((item1) => {
        newArr.forEach((item2) => {
          if (item1.value == item2) {
            checkList.push({ label: item1.label, value: item1.value })
          }
        })
      })
      this.setData({
        discuss: newList,
        selectedList: checkList,
      })
    })


    // console.log(this.data.defaultCheckList);
    // console.log(this.data.discuss);


  },
  changeselect(value, e) {
    console.log(this.$store.data);

    console.log(value);
    // console.log(this.data.discuss);
    let newArr = []
    this.data.discuss.forEach((item1) => {
      value.forEach((item2) => {
        if (item1.value == item2) {
          newArr.push({ label: item1.label, value: item1.value })
        }
      })
    });
    this.setData({
      group: value,
      selectedList: newArr
    })
  },
  cancelTap() {
    dd.navigateBack();
  },

  handleTap() {
    let pages = getCurrentPages();
    console.log(pages);
    if (pages.length > 1) {
      let prePage = pages[pages.length - 2];
      prePage.changeData(this.data.group);
      dd.navigateBack();
    }

    // console.log(this.$store.data.evaluationFormText);
    // console.log(this.data.selectedList);

    this.$store.data.launchShowList = this.data.selectedList
    if (this.data.selectedList.length != 0) {
      this.$store.data.evaluationFormText = '已选下方列表'
    } else {
      this.$store.data.evaluationFormText = '请选择'
    }
    this.update()
  }
});
